package top.jolyoulu.streaming

import org.apache.spark.SparkConf
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.receiver.Receiver
import org.apache.spark.streaming.{Seconds, StreamingContext}

import scala.util.Random

/**
 * @Author: JolyouLu
 * @Date: 2024/5/19 15:07
 * @Description
 */
object Spark01_SparkStreaming_State_Transform {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkStreaming")
    val ssc = new StreamingContext(sparkConf, Seconds(5))

    val lines: ReceiverInputDStream[String] = ssc.socketTextStream("192.168.88.100", 9999)
    //transform方法操作DStream的RDD
    //场景：DStream API不完善，需要使用到RDD，并且需要周期性的执行
    //Driver执行（main启动时走一遍）
    val newDS: DStream[String] = lines.transform(
      rdd => {
        //Driver端执行（周期性执行）
        rdd.map(
          str => {
            //Executor端执行
            str
          }
        )
      }
    )

    ssc.start()
    ssc.awaitTermination()
  }
}
